var bootbox_global;
var grid;

//删除
function deleteObj(objid)
{

    bootbox= bootbox_global;
    // alert(roleid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/user/deleteUserCase", "obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}

//删除
function deleteObj2(objid)
{

    bootbox= bootbox_global;
    // alert(roleid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/user/deleteUserCaseTask", "obj_id="+objid, function process_data(data) {




                        grid_show.refresh();


                    }, function process_fail(data) {

                        //操作失败
                        //AlertError(bootbox, data.msg);

                        alert( data.msg);


                    });

                }
            }
        }
    });

}



//修改对象
function updateObj(objid,datarow)
{

    bootbox= bootbox_global;

    var obj = grid.date_list[datarow];




    var alertarray = {
        form: {api: "/user/updateUserCase", name: "alertform", id: "alertform"},
        inputs: [

            {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name",value:obj.name},
            {title: "开始时间", type: "date", placeholder: "", verify: "empty", name: "start_day", id: "start_day",value:format_date_with_pattern(obj.start_day,"yyyy-MM-dd")},
            {title: "结束时间", type: "date", placeholder: "", verify: "empty", name: "end_day", id: "end_day",value:format_date_with_pattern(obj.end_day,"yyyy-MM-dd")},
            {
                title: "图片",
                type: "img",
                placeholder: "",
                verify: "empty",
                name: "picurl",
                id: "upimgs",
                src: obj.picurl
            },
            {title: "费用", type: "text", placeholder: "", verify: "empty", name: "fee", id: "fee",value:obj.fee},

            {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect",selectvalue:obj.type_id},
            {title: "评论", type: "text", placeholder: "", verify: "empty", name: "comment", id: "comment",value:obj.comment},


        ]

    }

    bootbox.dialog({
        title: "修改信息",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}


//查看对象
function viewObj(objid,datarow) {


    show_div_grid();

    var obj = grid.date_list[datarow];

    var columns = [
        {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

            return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
        }},

        { title: '阶段名', dataIndex: 'name',},
        { title: '工人名', dataIndex: 'worker_name',},
        { title: '天数', dataIndex: 'days',},
        { title: '材料', dataIndex: 'material',},

        { title: '图列表', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

            if(isNull( data[datarow].piclist))
                return "";
            var designpicarray = data[datarow].piclist.split(",");

            var html ="";

            for(var i=0;i<designpicarray.length;i++)
            {

                html +="<img class='girdimgthunb' onclick=\"viewUrl('"+designpicarray[i]+"')\" src='"+designpicarray[i]+"'/>&nbsp;";

            }

            return html;

        }},
        { title: '测试人', dataIndex: 'testor_name',},
        { title: '测试报告', dataIndex: 'test_content',},
        { title: '测试图片', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

            if(isNull( data[datarow].testpiclist))
                return "";
            var designpicarray = data[datarow].testpiclist.split(",");

            var html ="";

            for(var i=0;i<designpicarray.length;i++)
            {

                html +="<img class='girdimgthunb' onclick=\"viewUrl('"+designpicarray[i]+"')\" src='"+designpicarray[i]+"'/>&nbsp;";

            }

            return html;

        }},

        { title: '创建时间', dataIndex: 'create_time',},
        {
            title: '操作', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

            var html = "<div class='div_btn'><button onclick=\"updateObj2(" + data[datarow].id + "," + datarow + ")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj2(" + data[datarow].id + ")\"  class='btn btn-danger'>删除</button>";


            html = html + "</div>"

            return html;
        }
        },



    ];

    grid_show = new Grid("/user/getUserCaseTaskList", $("#table_my_toast1"), $("#paging_my_toast1"), 1, 10, columns, "&case_id="+obj.id, 10);

    //初始化
    grid_show.Init();



    return;



}


//查看对象
function addnewObj(objid,datarow) {

    //alert(objid);


    var bootbox = bootbox_global;
    var alertarray = {
        form: {api: "/user/addUseCaseTask", name: "alertform", id: "alertform"},
        inputs: [


            {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"},
            {title: "工人名", type: "text", placeholder: "", verify: "empty", name: "worker_name", id: "worker_name"},
            {title: "天数", type: "text", placeholder: "", verify: "empty", name: "days", id: "days"},


            {title: "材料", type: "text", placeholder: "", verify: "empty", name: "material", id: "material"},


            {title: "图1", type: "img", placeholder: "", verify: "empty", name: "construct1", id: "construct1",src:""},
            {title: "图2", type: "img", placeholder: "", verify: "", name: "construct2", id: "construct2",src:""},
            {title: "图3", type: "img", placeholder: "", verify: "", name: "construct3", id: "construct3",src:""},
            {title: "图4", type: "img", placeholder: "", verify: "", name: "construct4", id: "construct4",src:""},
            {title: "图5", type: "img", placeholder: "", verify: "", name: "construct5", id: "construct5",src:""},
            {title: "图6", type: "img", placeholder: "", verify: "", name: "construct6", id: "construct6",src:""},

            {title: "测试人", type: "text", placeholder: "", verify: "empty", name: "testor_name", id: "testor_name"},
            {title: "测试报告", type: "text", placeholder: "", verify: "empty", name: "test_content", id: "test_content"},

            {title: "测试图1", type: "img", placeholder: "", verify: "empty", name: "testpic1", id: "testpic1",src:""},
            {title: "测试图2", type: "img", placeholder: "", verify: "", name: "testpic2", id: "testpic2",src:""},
            {title: "测试图3", type: "img", placeholder: "", verify: "", name: "testpic3", id: "testpic3",src:""},
            {title: "测试图4", type: "img", placeholder: "", verify: "", name: "testpic4", id: "testpic4",src:""},
            {title: "测试图5", type: "img", placeholder: "", verify: "", name: "testpic5", id: "testpic5",src:""},
            {title: "测试图6", type: "img", placeholder: "", verify: "", name: "testpic6", id: "testpic6",src:""},





        ]

    }
    bootbox.dialog({
        title: "新增",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&case_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });



}



define([
    "jquery",
    "IO",
    "bootbox",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
], function ($, IO, bootbox) {


    $('#fileupload').fileupload({
        url: '/uploadUserCase',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {

        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = laoding_gif;

    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {

        console.log("upyuntype " + $('#fileupload').attr("upyuntype"));
        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = data._response.result.url;
    });


    bootbox_global = bootbox;
    $(function () {


        var columns_select ={name_dataIndex :'name',value_dataIndex :'id'};
         var  grid_select = new Select_Grid("/user/getUserCaseTypeList",$("#typeselect"),columns_select,"rows=100000");

                         grid_select.Init(function(){


                             dataInit();



                         });

        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
            }
            },
            //{ title: '种类id', dataIndex: 'type_id',},
            {title: '名称', dataIndex: 'name',},
            {
                title: '图片', dataIndex: 'gender', dataSource: function (data, datarow, gridobj, current_column) {

                var avatar = data[datarow].picurl;

                if(avatar.length == 0)
                    return "";
                else
                    return "<img class='girdimg img-thumbnail' onclick=\"viewUrl('"+avatar+"')\" src='"+avatar+"'/>"


            }
            },
            {title: '开始时间', dataIndex: 'start_day',},
            {title: '结束时间', dataIndex: 'end_day',},
            {title: '费用', dataIndex: 'fee',},
            {title: '评论', dataIndex: 'comment',},
            {
                title: '操作', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                var html = "<div class='div_btn'><button onclick=\"updateObj(" + data[datarow].id + "," + datarow + ")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj(" + data[datarow].id + ")\"  class='btn btn-danger'>删除</button>";


                html = html + "<button onclick=\"viewObj(" + data[datarow].id + "," + datarow + ")\" class='btn btn-info'>查看阶段</button>"


                html = html + "<button onclick=\"addnewObj(" + data[datarow].id + "," + datarow + ")\" class='btn btn-success'>添加阶段</button></div>"

                return html;
            }
            },




        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/user/getUserCaseList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "type_id=" + $("#typeselect").val();

            return param;

        }

       // dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });

        //新增
        $("#btn_add").bind("click", function () {


            var alertarray = {
                form: {api: "/user/addUserCase", name: "alertform", id: "alertform"},
                inputs: [


                    {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"},
                    {title: "开始时间", type: "date", placeholder: "", verify: "empty", name: "start_day", id: "start_day"},
                    {title: "结束时间", type: "date", placeholder: "", verify: "empty", name: "end_day", id: "end_day"},
                      {
                        title: "图片",
                        type: "img",
                        placeholder: "",
                        verify: "empty",
                        name: "picurl",
                        id: "upimgs",
                        src: ""
                    },

                    {title: "费用", type: "text", placeholder: "", verify: "empty", name: "fee", id: "fee"},

                    {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect"},
                    {title: "评论", type: "text", placeholder: "", verify: "empty", name: "comment", id: "comment"},




                    //{title: "图片", type: "img", placeholder: "", verify: "empty", name: "pic_url2", id: "upimgs2"},


                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });

    });


});